﻿using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using shenghua.Articles.Dto;
using shenghua.Basics;
using shenghua.Commodity.Commodities;
using shenghua.Commodity.Commodities.Dto;
using shenghua.Common.Articles;
using shenghua.Common.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Home
{
    public class HomeAppService: shenghuaAppServiceBase, IHomeAppService
    {
        private readonly WebsiteInfoManager _websiteInfoManager;
        private readonly CommodityManager _commodityManager;
        private readonly IRepository<Commodity.Commodities.Commodity> _commodityRepository;
        private readonly IRepository<CommodityShoppingOption> _shoppingOptionRepository;
        private readonly IRepository<Article> _articleRepository;

        public HomeAppService(
            WebsiteInfoManager websiteInfoManager,
            CommodityManager commodityManager,
            IRepository<Commodity.Commodities.Commodity> commodityRepository,
            IRepository<CommodityShoppingOption> shoppingOptionRepository,
            IRepository<Article> articleRepository
            )
        {
            _websiteInfoManager = websiteInfoManager;
            _commodityManager = commodityManager;
            _commodityRepository = commodityRepository;
            _articleRepository = articleRepository;
        }

        public async Task<object> GetIndex()
        {
            return new
            {
                Honor = _websiteInfoManager.GetHonor(),
                CompanyProfile = _websiteInfoManager.GetCompanyProfile(),
                Advertisements = _websiteInfoManager.GetAdvertisement(),
                Notices = _articleRepository.GetAll().Where(r => r.Enabled && r.Status == ArticleStatus.Audited && r.SystemType == ArticleSystemType.Notice).OrderByDescending(r => r.CreationTime).Take(5).Select(r => new
                {
                    r.Id,
                    r.Picture,
                    r.CreationTime,
                    r.Title,
                    r.Description
                }).ToList(),
                Commodities = _commodityRepository.GetAll().Where(r => r.Shelved).OrderByDescending(r => r.CreationTime).Take(18).Select(r => new
                {
                    r.Id,
                    r.Name,
                    r.ShoppingInfo.Picture
                }).ToList(),
                Articles = _articleRepository.GetAll().Where(r => r.Enabled && r.Status == ArticleStatus.Audited && r.SystemType == ArticleSystemType.CompanyInformation).OrderByDescending(r => r.CreationTime).Take(4).Select(r => new
                {
                    r.Id,
                    r.Picture,
                    r.CreationTime,
                    r.Title,
                    r.Description
                }).ToList()
            };
        }

        public async Task<object> GetFooter()
        {
            return new
            {
                CompanyProfile = _websiteInfoManager.GetCompanyProfile(),
                FriendlyLinks = _websiteInfoManager.GetFriendlyLink().Take(20).ToList(),
                WebsiteInfo = _websiteInfoManager.GetWebsiteInfo(),
                CompanyInfo = _websiteInfoManager.GetCompanyInfo()
            };
        }

        [HttpPost]
        public async Task<SearchResultDto<RealCommodityDto>> GetCommodityRecommendeds(SearchDto input)
        {
            var member = await GetCurrentMemberAsync(false);

            var query = _commodityRepository.GetAll().Where(r => r.Shelved);

            var items = query.OrderByDescending(r => r.ShoppingInfo.SalesVolume).Skip(input.SkipCount).Take(input.MaxResultCount).ToList();

            return new SearchResultDto<RealCommodityDto>
            {
                Items = items.Select(r => _commodityManager.GetRealCommodity(member, r, null).Result).Select(r => new RealCommodityDto(r)).ToList()
            };
        }

        [HttpPost]
        public async Task<SearchResultDto<ArticleDto>> GetArticleRecommendeds(SearchDto input)
        {
            var query = _articleRepository.GetAll().Where(r => r.Enabled && r.Status == ArticleStatus.Audited);

            var items = query.OrderByDescending(r => r.ClickCount).Skip(input.SkipCount).Take(input.MaxResultCount).ToList();

            return new SearchResultDto<ArticleDto>
            {
                Items = items.Select(r => new ArticleDto(r)).ToList()
            };
        }
    }
}
